//
//  LoginViewController.m
//  HTP
//
//  Created by 陈钱钿 on 15/11/6.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "LoginViewController.h"
#import "UserVM.h"
#import "AppDelegate.h"
#define kBorder 20
#define sHeight 40
@interface LoginViewController () <UITextFieldDelegate>
@property (nonatomic, retain) UITextField *userTextField;
@property (nonatomic, retain) UITextField *pwdTextField;
@property (nonatomic,retain) UITextField *editingTextfield;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = RGBCOLOR(233.0, 233.0, 233.0, 1);
    [self initUI];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBG"] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@"clearBg"]];
    [self addCustomBackBase];
    [self addCustomTitleWithTitle:@"登录"];
    self.userTextField.text = [USERCONTENT getUserLoginName];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHiden:) name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillHideNotification];
    [[NSNotificationCenter defaultCenter] removeObserver:UIKeyboardWillShowNotification];
    self.navigationController.navigationBarHidden = YES;
}
//TODO:绘制UI
- (void)initUI {
    
    UIView *userView = [[UIView alloc] initWithFrame:CGRectMake(15, 2*kBorder, ScreenWidth-2*15, sHeight)];
    [self imageViewWithFrame:CGRectMake(0, 0, ScreenWidth-2*15, sHeight) andImageName:@"bg_input" andPutView:userView];
    UIImageView *userIcon = [self imageViewWithFrame:CGRectMake(10, 10, 18, 20) andImageName:@"username" andPutView:userView];
    UIImageView *sline = [self imageViewWithFrame:CGRectMake(CGRectGetMaxX(userIcon.frame)+5, 10, 1, sHeight-2*10) andImageName:@"line_input" andPutView:userView];
    self.userTextField = [self textFieldWithFrame:CGRectMake(CGRectGetMaxX(sline.frame)+10, 0, 230, sHeight) placeholder:@"请输入用户名" andPutView:userView];
    [self.view addSubview:userView];
    
    UIView *pwdView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(userView.frame)+2*kBorder, ScreenWidth-2*15, sHeight)];
    [self imageViewWithFrame:CGRectMake(0, 0, ScreenWidth-2*15, sHeight) andImageName:@"bg_input" andPutView:pwdView];
    UIImageView *pwdIcon = [self imageViewWithFrame:CGRectMake(10, 10, 18, 20) andImageName:@"password" andPutView:pwdView];
    UIImageView *sline2 = [self imageViewWithFrame:CGRectMake(CGRectGetMaxX(pwdIcon.frame)+5, 10, 1, sHeight-2*10) andImageName:@"line_input" andPutView:pwdView];
    self.pwdTextField =  [self textFieldWithFrame:CGRectMake(CGRectGetMaxX(sline2.frame)+10, 0, 230, sHeight) placeholder:@"请输入登录密码" andPutView:pwdView];
    self.pwdTextField.secureTextEntry = YES;
    [self.view addSubview:pwdView];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    confirmBtn.frame = CGRectMake(15, CGRectGetMaxY(pwdView.frame)+2*kBorder, ScreenWidth-2*15, sHeight+2);
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"bg_bt"] forState:UIControlStateNormal];
    [confirmBtn addTarget:self action:@selector(confirmClick) forControlEvents:UIControlEventTouchUpInside];
    [confirmBtn setTitle:@"确 认" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:confirmBtn];
    
}
//TODO:对imageView的封装
- (UIImageView *)imageViewWithFrame:(CGRect)frame andImageName:(NSString *)name andPutView:(UIView *)view{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    imageView.image = [UIImage imageNamed:name];
    [view addSubview:imageView];
    return imageView;
}
//TODO:对textField的封装
- (UITextField *)textFieldWithFrame:(CGRect)frame placeholder:(NSString *)text andPutView:(UIView *)view {
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    textField.placeholder = text;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.delegate = self;
    [view addSubview:textField];
    return textField;
}
//TODO:确认按钮的点击
- (void)confirmClick {
    NSString *firstCode;

    if (self.userTextField.text.length == 0){
        [self showSVProgressFailed:@"用户名不能为空"];
        return;
    }
    firstCode = [self.userTextField.text substringToIndex:1];
    if((self.userTextField.text.length != 11) || ![firstCode isEqualToString:@"1"]){
        [self showSVProgressFailed:@"手机格式错误"];
        return;
    }
    else if(self.pwdTextField.text.length==0){
        [self showSVProgressFailed:@"密码不能为空"];
        return;
    }else {
        [self loginWSDL];
    }

}

//TODO:登录调用接口 cqt
- (void)loginWSDL {
    UserVM *userVM = [[UserVM alloc] init];
    [userVM setBlockWithReturnBlock:^(id returnValue) {
        [self dismissSVProgress];
        NSDictionary *dict = returnValue[INTERFACE_INFO];
        NSLog(@"%@",dict);
        [USERCONTENT setUserBean:dict];
        [USERCONTENT setUserName:self.userTextField.text];
         [self changeRoot];
    } WithErrorBlock:^(id errorCode) {
        [self showSVProgressFailed:@"用户名密码错误"];
    } WithFailureBlock:^{
        [self showSVProgressFailed:INTERFACE_FAILURE_CHIN];
    }];
    [self showSvprogress:@""];
    [userVM userLogin:self.userTextField.text andPwd:self.pwdTextField.text withUserType:@"2"];
}

/*
 更改RootViewController
 */
-(void)changeRoot{
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app changeRootVc];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    _editingTextfield = textField;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self.view endEditing:YES];
    _editingTextfield = nil;
    return YES;
}
#pragma mark - keyboard
-(void) keyboardWillHiden:(NSNotification *)notification{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame = CGRectMake(0.0f, NavHe, ScreenWidth, ScreenHeihgt);
    }];
}
-(void)keyboardWillShow:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
    CGFloat bottom = CGRectGetMaxY(_editingTextfield.frame);
    CGFloat keyboardOffset = CGRectGetHeight(keyboardRect);
    int offset = bottom + keyboardOffset + 1.0 - ScreenHeihgt;
    if(offset>0){
        [UIView animateWithDuration:animationDuration animations:^{
            self.view.frame = CGRectMake(0.0f, -offset, ScreenWidth, ScreenHeihgt);
        }];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
